<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="rootDialogFormRef"  :rules="state.rules"  :model="state.ruleForm" size="default" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="省市县"  prop="proCityArea">
							<el-cascader
								:options="cityJson"
								:props="{ checkStrictly: true, value: 'name', label: 'name' }"
								placeholder="请选择省市县"
								clearable
								class="w100"

								v-model="state.ruleForm.proCityArea"
							>
<!--                       :rules="[
                  { required: true, message: '省市县不能为空'}
                ]"-->
								<template #default="{ node, data }">
									<span>{{ data.name }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
          <el-col :xs="48" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="根证书编号"  prop="numb"  label-width="100">
              <el-input-number v-model="state.ruleForm.numb" placeholder="" clearable></el-input-number>
            </el-form-item>
          </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="街道"  prop="street">
							<el-input v-model="state.ruleForm.street" placeholder="" clearable></el-input>
						</el-form-item>
					</el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="园区"  prop="village">
              <el-input v-model="state.ruleForm.village" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="组织"  prop="organize">
              <el-input v-model="state.ruleForm.organize" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="单元"  prop="unit">
              <el-input v-model="state.ruleForm.unit" placeholder="" clearable></el-input>
						</el-form-item>
					</el-col>

				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemMenuDialog">
import {reactive, onMounted, ref, nextTick} from 'vue';
import {addMiddle} from '/@/api/cec/cert';
import  cityJson from  '/@/assets/json/city.json';
import {ElMessageBox} from "element-plus";


// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 引入组件


// 定义变量内容
const rootDialogFormRef = ref();

const state = reactive({
  proCityArea: [], // 省市县级联
	// 参数请参考 `/src/router/route.ts` 中的 `dynamicRoutes` 路由菜单格式
	ruleForm: {
    proCityArea: [], // 省市县级联
    country:'中国',//国家
    province: '', // 省份
		city: '', // 城市
		area: '', // 市区
    street: '', // 街道
    village: '', // 村镇
    organize: '', // 组织
    unit: '', // 单元
    extname:'',//扩展信息
    numb:0,
	},
  rules: {
    proCityArea: [{ required: true, message: '请选择省市区县', trigger: 'blur' }],
    street: [{ required: true, message: '请输入街道', trigger: 'blur' }],
    village: [{ required: true, message: '请输入村镇', trigger: 'blur' }],
    organize: [{ required: true, message: '请输入组织', trigger: 'blur' }],
    unit: [{ required: true, message: '请输入单元', trigger: 'blur' }],
    extname: [{ required: true, message: '请输入扩展信息', trigger: 'blur' }],
    numb: [{ required: true, message: '请输入根证书编号', trigger: 'blur' }],

  },
	//menuData: [] as RouteItems, // 上级菜单数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 获取 pinia 中的路由
// const getMenuData = (routes: RouteItems) => {
// 	const arr: RouteItems = [];
// 	routes.map((val: RouteItem) => {
// 		val['title'] = i18n.global.t(val.meta?.title as string);
// 		arr.push({ ...val });
// 		if (val.children) getMenuData(val.children);
// 	});
// 	return arr;
// };
// 打开弹窗
const openDialog = (type: string, row?: any) => {
	if (type === 'edit') {
		// 模拟数据，实际请走接口
		row.menuType = 'menu';
		row.menuSort = Math.floor(Math.random() * 100);
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.dialog.title = '修改菜单';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增根证书';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		nextTick(() => {
			rootDialogFormRef.value.resetFields();
		});
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};


// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async() => {
  const valid = await rootDialogFormRef.value.validate().catch(() => {}); // 表单校验
  if (valid) {
    state.ruleForm.province=state.ruleForm.proCityArea[0];
    state.ruleForm.city=state.ruleForm.proCityArea[1];
    state.ruleForm.area=state.ruleForm.proCityArea[2];

    addMiddle({"info":state.ruleForm,"numb":state.ruleForm.numb}).then((result:any)=>{
      if(result.code==0){
        closeDialog(); // 关闭弹窗
        emit('refresh');

      }else{
        ElMessageBox.alert("中级证书编号不存在，请重新输入！")
      }
    });
  } else {
    console.log("校验失败")
  }



	// if (state.dialog.type === 'add') { }
	// setBackEndControlRefreshRoutes() // 刷新菜单，未进行后端接口测试
};
// 页面加载时
onMounted(() => {
	//state.menuData = getMenuData(routesList.value);
});

// 暴露变量
defineExpose({
	openDialog,
});
</script>
